#pragma once

#include <fcntl.h>
#include <iostream>
#include <linux/videodev2.h>
#include <opencv2/opencv.hpp>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <unistd.h>
// 用于存储视频帧的数据结构
struct FrameBuffer {
  void *start;
  size_t length;
};

const unsigned int num_buffers = 3;
class V4L2CamCTL {
private:
  int fd;
  int index = 0;
  struct FrameBuffer buffers[num_buffers];
  uint32_t fmt = V4L2_PIX_FMT_MJPEG;
  int open_device(const char *device_name);
  int query_device_capabilities();
  int set_format(unsigned int width, unsigned int height, uint32_t fmt);
  int request_buffers(unsigned int count, struct FrameBuffer *buffers);
  int start_capture();
  int stop_capture();

public:
  V4L2CamCTL(const char *device_name);
  int init_device();
  int read_frame(cv::Mat &frame);
  int set_camera_zoom(int zoom);
  int set_camera_exposure(int exposure);
  int set_camera_exposure_auto();
  int set_camera_auto_focus(bool enable);
  int set_camera_focus(int focus_value);
  ~V4L2CamCTL();
};
